package com.h2o3.carmack.network;

import org.glassfish.grizzly.http.HttpRequestPacket;
import org.glassfish.grizzly.websockets.Broadcaster;
import org.glassfish.grizzly.websockets.DataFrame;
import org.glassfish.grizzly.websockets.ProtocolHandler;
import org.glassfish.grizzly.websockets.WebSocket;
import org.glassfish.grizzly.websockets.WebSocketApplication;
import org.glassfish.grizzly.websockets.WebSocketListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.h2o3.carmack.message.MessageDispatcher;
import com.h2o3.carmack.message.MessageFactory;
import com.h2o3.carmack.message.MessageSerializer;
import com.h2o3.carmack.session.SessionFactory;

/**
 * @author chensheng
 * @version $Id: CarmackWebSocketApplication.java, v 0.1 1/18/14 8:28 PM chensheng Exp $
 */
public class CarmackWebSocketApplication extends WebSocketApplication {
    private static Logger     logger = LoggerFactory.getLogger(CarmackWebSocketApplication.class);

    private Broadcaster       broadcaster;

    private MessageDispatcher messageDispatcher;

    private MessageFactory    messageFactory;

    private MessageSerializer messageSerializer;

    @Override
    public WebSocket createSocket(ProtocolHandler handler, HttpRequestPacket requestPacket,
                                  WebSocketListener... listeners) {
        CarmackWebSocket webSocket = new CarmackWebSocket(handler, requestPacket, broadcaster,
            listeners);
        webSocket.setSession(SessionFactory.getInstance().createSession(webSocket,
            messageSerializer));

        return webSocket;
    }

    @Override
    public void onConnect(WebSocket socket) {
        super.onConnect(socket);

        messageDispatcher.dispatch(((CarmackWebSocket) socket).getSession(),
            messageFactory.createMessage("connect", null));
    }

    @Override
    public void onClose(WebSocket socket, DataFrame frame) {
        super.onClose(socket, frame);

        messageDispatcher.dispatch(((CarmackWebSocket) socket).getSession(),
            messageFactory.createMessage("disconnect", null));
    }

    @Override
    public void onMessage(WebSocket socket, String text) {
        messageDispatcher.dispatch(((CarmackWebSocket) socket).getSession(),
            messageSerializer.readMessage(text));
    }

    @Override
    protected boolean onError(WebSocket webSocket, Throwable t) {
        logger.error("请求处理中发生异常", t);
        return true;
    }

    public Broadcaster getBroadcaster() {
        return broadcaster;
    }

    public void setBroadcaster(Broadcaster broadcaster) {
        this.broadcaster = broadcaster;
    }

    public MessageDispatcher getMessageDispatcher() {
        return messageDispatcher;
    }

    public void setMessageDispatcher(MessageDispatcher messageDispatcher) {
        this.messageDispatcher = messageDispatcher;
    }

    public MessageFactory getMessageFactory() {
        return messageFactory;
    }

    public void setMessageFactory(MessageFactory messageFactory) {
        this.messageFactory = messageFactory;
    }

    public MessageSerializer getMessageSerializer() {
        return messageSerializer;
    }

    public void setMessageSerializer(MessageSerializer messageSerializer) {
        this.messageSerializer = messageSerializer;
    }
}
